#include "menu.h"
#include <string.h>
#include <stdlib.h>
#include <glib/gi18n.h>
#define CHARSET "utf-8"
extern gboolean app_exist;
extern GtkWidget *window;
extern gchar *p_app;
extern GKeyFile *uniconfig;
extern GKeyFileFlags flags;
extern GError *error;
extern gchar *ufile_buf;
extern gsize ulength;
extern gchar *user_home;
void checkhide(GtkWidget *wid, gpointer data)
{
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wid)))	
		g_key_file_set_integer (uniconfig, "UI", "visible", 0);
	else	g_key_file_set_integer (uniconfig, "UI", "visible", 1);
}

void checkAS(GtkWidget *wid, gpointer data)
{
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wid)))
		g_key_file_set_integer (uniconfig, "proxy", "autostart", 1);
	else	g_key_file_set_integer (uniconfig, "proxy", "autostart", 0);
}

void checkST(GtkWidget *wid, gpointer data)
{
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wid)))
	{
		g_key_file_set_integer (uniconfig, "UI", "skiptaskbar", 1);
		gtk_window_set_skip_taskbar_hint(GTK_WINDOW(window), TRUE);
	}
	else
	{
		g_key_file_set_integer (uniconfig, "UI", "skiptaskbar", 0);
		gtk_window_set_skip_taskbar_hint(GTK_WINDOW(window), FALSE);
	}
}

void check_gae()
{
	if(access(p_app, 00) == 0)	app_exist = TRUE;
	else{
		app_exist = FALSE;
		perror(_("Backend path incorrect"));
	}
}

void checkONE(GtkWidget *wid, gpointer data)
{
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wid)))
	{
		g_key_file_set_integer (uniconfig, "UI", "oneup", 1);
	}
	else
	{
		g_key_file_set_integer (uniconfig, "UI", "oneup", 0);
	}
}

void save_setup(GtkWidget *wid, gpointer data)
{
	p_app = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(chooserbutton));
	g_key_file_set_string(uniconfig, "backend", "path", p_app);
	ufile_buf = g_key_file_to_data(uniconfig, &ulength, &error);
	g_file_set_contents(user_home, ufile_buf, -1, &error);
	if(error != NULL)
	{
		perror(_("Error occur while saving"));
		g_free(error);
	}
	gtk_widget_destroy(data);
}

void cancel_setup(GtkWidget *wid, gpointer data)
{
	gtk_widget_destroy(data);
}

void show_setup(GtkWidget *wid, gpointer data)
{
	GtkWidget *swindow;
	GtkWidget *hbox;
	GtkWidget *vbox;
	GtkWidget *slabel;
	GtkWidget *button;
	GtkWidget *check;
	GtkFileFilter *filter;
	
	//i18n
	bindtextdomain ("uniproxy-menu", "/usr/share/locale");
	bind_textdomain_codeset ("uniproxy-menu", CHARSET);
	textdomain ("uniproxy-menu");

	swindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_resizable(GTK_WINDOW(swindow), FALSE);
	gtk_window_set_title (GTK_WINDOW (swindow), _("Setup"));
	gtk_window_set_position (GTK_WINDOW (swindow), GTK_WIN_POS_CENTER);
	
	vbox = gtk_vbox_new (TRUE, 6);
	gtk_container_add (GTK_CONTAINER (swindow), vbox);
	check = gtk_check_button_new_with_label (_("Start Uniproxy without showing main window"));
	if(g_key_file_get_integer(uniconfig, "UI", "visible", NULL)==0){
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), TRUE);}
	g_signal_connect(G_OBJECT(check), "toggled", G_CALLBACK(checkhide), NULL);
	gtk_box_pack_start(GTK_BOX (vbox), check, TRUE, TRUE, 2);
	
	check = gtk_check_button_new_with_label (_("Start proxy daemon automatically."));
	if(g_key_file_get_integer(uniconfig, "proxy", "autostart", NULL)!=0){
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), TRUE);}
	g_signal_connect(G_OBJECT(check), "toggled", G_CALLBACK(checkAS), NULL);
	gtk_box_pack_start(GTK_BOX (vbox), check, TRUE, TRUE, 2);

	check = gtk_check_button_new_with_label (_("Hide the taskbar."));
	if(g_key_file_get_integer(uniconfig, "UI", "skiptaskbar", NULL)!=0){
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), TRUE);}
	g_signal_connect(G_OBJECT(check), "toggled", G_CALLBACK(checkST), NULL);
	gtk_box_pack_start(GTK_BOX (vbox), check, TRUE, TRUE, 2);

	check = gtk_check_button_new_with_label (_("Only ONE Uniproxy running."));
	if(g_key_file_get_integer(uniconfig, "UI", "oneup", NULL)!=0){
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), TRUE);}
	g_signal_connect(G_OBJECT(check), "toggled", G_CALLBACK(checkONE), NULL);
	gtk_box_pack_start(GTK_BOX (vbox), check, TRUE, TRUE, 2);

	button = gtk_button_new_from_stock(GTK_STOCK_SAVE);
	hbox = gtk_hbox_new (FALSE, 2);
	slabel = gtk_label_new (_("Backend:"));
	chooserbutton = gtk_file_chooser_button_new (_("Choose the py file"), GTK_FILE_CHOOSER_ACTION_OPEN);
	gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(chooserbutton), p_app);
	filter = gtk_file_filter_new();
	gtk_file_filter_set_name(filter, _("py Files"));
	gtk_file_filter_add_pattern(filter, "*.py");
	gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(chooserbutton), filter);
	gtk_box_pack_start(GTK_BOX (vbox), hbox, TRUE, TRUE, 2);
	gtk_box_pack_start(GTK_BOX (hbox), slabel, FALSE, FALSE, 2);
	gtk_box_pack_start(GTK_BOX (hbox), chooserbutton, TRUE, TRUE, 2);
	hbox = gtk_hbox_new (TRUE, 2);
	gtk_box_pack_start(GTK_BOX (vbox), hbox, TRUE, TRUE, 2);
	gtk_box_pack_start(GTK_BOX (hbox), button, TRUE, TRUE, 2);
	g_signal_connect(button, "clicked", G_CALLBACK(save_setup), G_OBJECT(swindow));
	button = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
	gtk_box_pack_start(GTK_BOX (hbox), button, TRUE, TRUE, 2);
	g_signal_connect(button, "clicked", G_CALLBACK(cancel_setup), G_OBJECT(swindow));
	g_signal_connect(G_OBJECT(window), "delete-event", G_CALLBACK(cancel_setup), G_OBJECT(swindow));
	gtk_widget_show_all (swindow);
}

void show_about(GtkWidget *wid, gpointer data)
{
	GtkWidget *dialog;
	dialog = gtk_about_dialog_new();
	
	gtk_about_dialog_set_program_name(GTK_ABOUT_DIALOG(dialog), "Uniproxy-GUI");
	gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(dialog), "0.2.8a");
	gtk_about_dialog_set_copyright(GTK_ABOUT_DIALOG(dialog), "© William Wong");
	gtk_about_dialog_set_license(GTK_ABOUT_DIALOG(dialog), "GNU通用公共许可证\n第三版 2007年6月29日\n版权所有（C）2007 Free Software Foundation, Inc. (http://fsf.org)\n允许所有人复制和发布本授权文件的完整版本\n但不允许对它进行任何修改\n全文见:\nhttp://sites.google.com/site/bergwolf02/gplv3_zh (中文)\nhttp://www.gnu.org/licenses/gpl-3.0.html (English)\nFollow me on Twitter: @librehat");
	gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(dialog), "http://code.google.com/p/uniproxy-gui");
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}
